/
ImageBrightnessTransform.cs
93 lines (78 loc) · 2.98 KB
/
ImageBrightnessTransform.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace DotNetNuke.Services.GeneratedImage.FilterTransform
{
using System.Drawing;
using System.Drawing.Drawing2D;
/// <summary>Brightness ImageTransform class.</summary>
public class ImageBrightnessTransform : ImageTransform
{
/// <summary>Initializes a new instance of the <see cref="ImageBrightnessTransform"/> class.</summary>
public ImageBrightnessTransform()
{
this.InterpolationMode = InterpolationMode.HighQualityBicubic;
this.SmoothingMode = SmoothingMode.HighQuality;
this.PixelOffsetMode = PixelOffsetMode.HighQuality;
this.CompositingQuality = CompositingQuality.HighQuality;
this.Brightness = 0;
}
/// <summary>Gets provides an Unique String for this class.</summary>
public override string UniqueString => base.UniqueString + "-" + this.Brightness;
/// <summary>Gets or sets the brightness value. Defaultvalue is 0. Range is -255 .. 255.</summary>
public int Brightness { get; set; }
/// <summary>Processes an input image applying a brightness image transformation.</summary>
/// <param name="image">Input image.</param>
/// <returns>Image result after image transformation.</returns>
public override Image ProcessImage(Image image)
{
var temp = (Bitmap)image;
var bmap = (Bitmap)temp.Clone();
if (this.Brightness < -255)
{
this.Brightness = -255;
}
if (this.Brightness > 255)
{
this.Brightness = 255;
}
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
int cR = c.R + this.Brightness;
int cG = c.G + this.Brightness;
int cB = c.B + this.Brightness;
if (cR < 0)
{
cR = 1;
}
if (cR > 255)
{
cR = 255;
}
if (cG < 0)
{
cG = 1;
}
if (cG > 255)
{
cG = 255;
}
if (cB < 0)
{
cB = 1;
}
if (cB > 255)
{
cB = 255;
}
bmap.SetPixel(i, j, Color.FromArgb((byte)cR, (byte)cG, (byte)cB));
}
}
return (Bitmap)bmap.Clone();
}
}
}